package com.sqlcheck.entity;

import com.baomidou.mybatisplus.annotation.*;
import com.sqlcheck.util.JsonUtils;
import lombok.Getter;
import lombok.Setter;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Map;

@Getter
@Setter
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName(value = "scan_results")
public class ScanResult {

    @TableId(value = "id", type = IdType.AUTO)
    private Long id;

    @TableField("task_id")
    private Long taskId;

    @TableField("file_path")
    private String filePath;

    @TableField("file_type")
    private FileType fileType;

    @TableField("sql_content")
    private String sqlContent;

    @TableField("sql_type")
    private SqlType sqlType;

    @TableField("line_number")
    private Integer lineNumber;

    @TableField("column_number")
    private Integer columnNumber;

    @TableField("issue_type")
    private IssueType issueType;

    @TableField("severity")
    private Severity severity;

    @TableField("rule_id")
    private String ruleId;

    @TableField("rule_name")
    private String ruleName;

    @TableField("issue_description")
    private String issueDescription;

    @TableField("suggestion")
    private String suggestion;

    @TableField("original_sql")
    private String originalSql;

    @TableField("suggested_sql")
    private String suggestedSql;

    @TableField("metadata")
    private String metadataJson;

    @TableField("statement_id")
    private Long statementId;

    @TableField("statement_number")
    private Integer statementNumber;

    @TableField("analysis_engine")
    private String analysisEngine;

    @TableField("ai_optimized")
    private Boolean aiOptimized;

    @TableField("ai_confidence_score")
    private BigDecimal aiConfidenceScore;

    @TableField("optimized_sql")
    private String optimizedSql;

    @TableField("optimization_explanation")
    private String optimizationExplanation;

    @TableField("rule_analysis_confirmed")
    private Boolean ruleAnalysisConfirmed;

    @TableField("ai_issue_description")
    private String aiIssueDescription;

    @TableField("ai_verification_details")
    private String aiVerificationDetailsJson;

    @TableField(value = "created_at", fill = FieldFill.INSERT)
    private LocalDateTime createdAt;

    @TableLogic
    @TableField("deleted")
    private Boolean deleted;

    // Helper methods for metadata JSON field
    public Map<String, Object> getMetadata() {
        return JsonUtils.safeParseMap(this.metadataJson);
    }

    public ScanResult setMetadata(Map<String, Object> metadata) {
        this.metadataJson = JsonUtils.safeToJsonString(metadata);
        return this;
    }

    // Helper methods for AI verification details JSON field
    public Map<String, Object> getAiVerificationDetails() {
        return JsonUtils.safeParseMap(this.aiVerificationDetailsJson);
    }

    public ScanResult setAiVerificationDetails(Map<String, Object> details) {
        this.aiVerificationDetailsJson = JsonUtils.safeToJsonString(details);
        return this;
    }

    public enum FileType {
        JAVA, XML, SQL, PROPERTIES, OTHER
    }

    public enum SqlType {
        QUERY, DML, DDL, PROCEDURE, FUNCTION, TRIGGER
    }

    public enum IssueType {
        SYNTAX_ERROR,           // 语法错误
        COMPATIBILITY_WARNING,  // 兼容性警告
        PERFORMANCE_ISSUE,      // 性能问题
        SECURITY_RISK          // 安全风险
    }

    public enum Severity {
        HIGH, MEDIUM, LOW, INFO
    }
}